<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="showAddCateDialog">添加商品</el-button>
      <tree-table
        border
        :show-index="true"
        index-text="#"
        :selection-type="false"
        :expand-type="false"
        :data="cateList"
        :columns="columns"
      >
        <template slot="isOK" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: green"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="level" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level=== 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level=== 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加商品对话框 -->
    <el-dialog
      @close="addCateDialogClose"
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="50%"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRule"
        ref="addCateFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择 -->
        <!-- v-model表示当前选择的分类d的id 保存在selectOptionArr里面 options为数据源  props为选择器的配置项-->
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectOptionArr"
            :options="parentCate"
            :props="cascaderProps"
            @change="selectOptionChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateSub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 用来查询分类页的参数
      queryData: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      cateList: [],
      total: 0,
      //可使用type来设置为template 为自定义列
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isOK"
        },
        {
          label: "等级",
          type: "template",
          template: "level"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],

      addCateDialogVisible: false,
      //添加的分类对象存储在这里
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      //添加分类表单的验证规则对象
      addCateFormRule: {
        cat_name: [
          { required: true, message: "请输入名称名称", trigger: "blur" }
        ]
      },

      //存放父级分类 添加分类时 级联选择框渲染父级分类使用
      parentCate: [],

      selectOptionArr: [],
      // 级联选择器 的配置项  checkStrictly为新的属性为true则可以选择任意一级分类 为false只能选择最后一级
      cascaderProps: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true
      }
    };
  },

  methods: {
    async getGoodsCate() {
      var res = await this.axios.get("categories", {
        params: this.queryData
      });
      console.log(res);
      if (res.meta.status == 200) {
        this.cateList = res.data.result;
        this.total = res.data.total;
      }
    },

    handleSizeChange(v) {
      this.queryData.pagesize = v;
      this.getGoodsCate();
    },
    handleCurrentChange(v) {
      this.queryData.pagenum = v;
      this.getGoodsCate();
    },

    //展示添加分类对话框
    async showAddCateDialog() {
      this.addCateDialogVisible = true;
      // 发送请求获取父级分类 一级二级
      var res = await this.axios.get("categories", {
        params: {
          type: 2
        }
      });
      console.log(res);
      if (res.meta.status == 200) {
        this.parentCate = res.data;
      }
    },

    // 级联选择器的选项发生变化时触发
    selectOptionChange() {
      // 拿到选择的父级分类id
      console.log(this.selectOptionArr);
      if (this.selectOptionArr.length > 0) {
        //如果选择了父级分类
        this.addCateForm.cat_pid = this.selectOptionArr[
          this.selectOptionArr.length - 1
        ];
        this.addCateForm.cat_level = this.selectOptionArr.length;
      } else {
        //如果 没有选择任何父类 默认为添加一级分类
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },

    // 点击确定添加分类
    addCateSub() {
      // 先做表单验证
      this.$refs.addCateFormRef.validate(async flag => {
        if (!flag) return;
        var res = await this.axios.post("categories", this.addCateForm);
        console.log(res);
        if (res.meta.status == 201) {
          this.$message.success("添加成功");
          this.getGoodsCate();
          this.addCateDialogVisible = false;
        } else {
          this.$message.success("添加失败");
        }
      });
    },

    // 关闭添加分类对话框时 重置表单 清空之前操作保存的数据
    addCateDialogClose() {
      //重置表单
      this.$refs.addCateFormRef.resetFields();
      // 清空数组保存的选中的id
      this.selectOptionArr = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    }
  },

  created() {
    this.getGoodsCate();
  }
};
</script>

<style lang='less' scoped>
</style>
